package io.xccit.hotelsystem.ui.order;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

import io.xccit.hotelsystem.R;
import io.xccit.hotelsystem.utils.SQLiteHelper;

/**
 * 订单详情
 */
public class OrderDetailActivity extends AppCompatActivity {

    private static final String TAG = "OrderDetailActivity:";
    private SQLiteHelper dbHelper;
    private int orderId;
    private int orderStatus;
    
    @SuppressLint("Range")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        
        dbHelper = new SQLiteHelper(this);
        orderId = getIntent().getIntExtra("order_id", -1);
        Log.d(TAG, "onCreate: orderId:" + orderId);
        
        // 初始化视图
        TextView tvHotelName = findViewById(R.id.tvHotelName);
        TextView tvOrderStatus = findViewById(R.id.tvOrderStatus);
        TextView tvStartTime = findViewById(R.id.tvStartTime);
        TextView tvEndTime = findViewById(R.id.tvEndTime);
        TextView tvPrice = findViewById(R.id.tvPrice);
        TextView tvOrderName = findViewById(R.id.tvOrderName);
        TextView tvOrderPhone = findViewById(R.id.tvOrderPhone);
        Button btnCancelOrder = findViewById(R.id.btnCancelOrder);
        
        // 加载订单数据
        Cursor cursor = dbHelper.getOrderById(orderId);
        if (cursor.moveToFirst()) {
            tvHotelName.setText(cursor.getString(cursor.getColumnIndex("hotel_name")));
            orderStatus = cursor.getInt(cursor.getColumnIndex("order_status"));
            tvOrderStatus.setText(getStatusText(orderStatus));
            tvStartTime.setText("入住时间: " + cursor.getString(cursor.getColumnIndex("start_time")));
            tvEndTime.setText("离店时间: " + cursor.getString(cursor.getColumnIndex("end_time")));
            tvPrice.setText("价格: " + cursor.getDouble(cursor.getColumnIndex("price")) + "元");
            tvOrderName.setText("预定人: " + cursor.getString(cursor.getColumnIndex("name")));
            tvOrderPhone.setText("联系电话: " + cursor.getString(cursor.getColumnIndex("phone")));
            
            // 设置按钮状态
            if (orderStatus == 1) { // 未入住
                btnCancelOrder.setEnabled(true);
                btnCancelOrder.setBackgroundTintList(getResources().getColorStateList(R.color.warm_green_200));
                btnCancelOrder.setOnClickListener(v -> {
                    dbHelper.updateOrderStatus(orderId, 3); // 3表示已退订
                    finish();
                });
            } else if (orderStatus == 2) { // 已完成
                btnCancelOrder.setEnabled(false);
                btnCancelOrder.setBackgroundTintList(getResources().getColorStateList(R.color.gray));
            } else if (orderStatus == 3) { // 已退订
                btnCancelOrder.setEnabled(false);
                btnCancelOrder.setBackgroundTintList(getResources().getColorStateList(R.color.gray));
            }
        }
        cursor.close();
    }
    
    private String getStatusText(int status) {
        switch (status) {
            case 1: return "订单状态: 未入住";
            case 2: return "订单状态: 已完成";
            case 3: return "订单状态: 已退订";
            default: return "订单状态: 未知";
        }
    }
}